package com.realgodo.site.service.site;

import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.site.Info;


@Service("infoManager")
@Transactional
public class InfoManager extends DefaultEntityManager<Info, Long> {

	@Autowired
	protected SessionFactory sessionFactory;
	
	public Page<Info> find(int pageSize,Map<String,Object> param) {
		Page<Info> page = new Page<Info>(pageSize);
		Criteria c = sessionFactory.getCurrentSession().createCriteria(Info.class);
		if(param==null){
			page.setPageNo(1);
			return this.entityDao.findByCriteria(page, c);
		}
		page.setPageNo(super.isParamNotNull(param, Page.PAGE)?Integer.valueOf((String)getParamValue(param,  Page.PAGE)):1);//查询页数
		param.remove(Page.PAGE);
		if(isParamNotNull(param, "status")){
			c.add(Restrictions.eq("status", Integer.valueOf((String)getParamValue(param, "status")).intValue()));
			if(Integer.valueOf((String)getParamValue(param, "status")).intValue()==Info.STATUS_PUB){
				c.addOrder(Order.desc("pubDate"));
			}
		}
		c.addOrder(Order.desc("id"));
		return this.entityDao.findByCriteria(page, c);
	}
}
